package com.zy.fee.roadpark.dao;

import android.content.Context;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;
import com.yanzhenjie.nohttp.rest.StringRequest;
import com.zy.fee.roadpark.config.Urls;
import com.zy.fee.roadpark.model.apimodel.APIM_CarNumberList;
import com.zy.fee.roadpark.model.apimodel.APIM_NodeRecordInfo;
import com.zy.fee.roadpark.model.apimodel.APIM_NodeRecordList;
import com.zygk.library.model.CommonResult;
import com.zygk.library.util.CallServer;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.JsonUtil;
import com.zygk.library.util.PreferencesHelper;
import com.zygk.library.util.ToastUtil;

/**
 * Created by Administrator on 2018/9/13 0013.
 */

public class NbNodeRecordLogic {
  /**
   * 根据车牌号获取未支付的停车单列表
   */
  public static void nbnode_record_unpay_list(final Context mContext, String carNumber, int page,
      int rows, final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NBNODE_RECORD_UNPAY_LIST, RequestMethod.POST);
    request.add("carNumber", carNumber);
    request.add("page", page);
    request.add("rows", rows);

    HttpRequest.net(request, new HttpRequest.HttpListener() {
      @Override public void onStart(int what) {
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response response) {
        APIM_NodeRecordList result =
            JsonUtil.jsonToObject(response.get().toString(), APIM_NodeRecordList.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response response) {
        callback.onFailed();

      }

      @Override public void onFinish(int what) {
        callback.onFinish();

      }
    });
  }

  /**
   * 获取地磁停车单详情
   */
  public static void nbnode_record_info(final Context mContext, String nodeRecordID,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NBNODE_RECORD_INFO, RequestMethod.POST);
    request.add("nodeRecordID", nodeRecordID);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_NodeRecordInfo result =
            JsonUtil.jsonToObject(response.get(), APIM_NodeRecordInfo.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 管理员确认线下收款
   */
  public static void nbnode_record_pay_admin(final Context mContext, String adminID,
      String nodeRecordID, final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NBNODE_RECORD_PAY_ADMIN, RequestMethod.POST);
    request.add("adminID", adminID);
    request.add("nodeRecordID", nodeRecordID);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result = JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 收款(根据车位号和车牌号获取未支付的最新的停车单)
   */
  public static void nbnode_searchrecord_unpay(final Context mContext, String adminID, String code,
      String carNumber, final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NBNODE_SEARCHRECORD_UNPAY, RequestMethod.POST);
    request.add("adminID", adminID);
    request.add("code", code);
    request.add("carNumber", carNumber);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_NodeRecordInfo result =
            JsonUtil.jsonToObject(response.get(), APIM_NodeRecordInfo.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 获取管理员管理的地磁设备中正在停车的且已取证的所有车辆的所有历史欠费单据。
   */
  public static void nbnode_record_histroy_admin_list(final Context mContext,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NBNODE_RECORD_HISTROY_ADMIN_LIST, RequestMethod.POST);
    request.add("adminID", PreferencesHelper.userManager().getUserID());
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_CarNumberList result =
            JsonUtil.jsonToObject(response.get(), APIM_CarNumberList.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 根据车位号获取最新的未支付的地磁停车单(未取证)
   */
  public static void nbnode_record_unpay_code(final Context mContext, String code,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NBNODE_RECORD_UNPAY_CODE, RequestMethod.POST);
    request.add("code", code);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_NodeRecordInfo result =
            JsonUtil.jsonToObject(response.get(), APIM_NodeRecordInfo.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 结束地磁停车单
   */
  public static void nbnode_record_end(final Context mContext, String nodeRecordID,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NBNODE_RECORD_END, RequestMethod.POST);
    request.add("adminID", PreferencesHelper.userManager().getUserID());
    request.add("nodeRecordID", nodeRecordID);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result =
            JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }

  /**
   * 结束并新增地磁停车单
   */
  public static void nbnode_record_refresh(final Context mContext, String nodeRecordID, String carNumber, String pic,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(Urls.NBNODE_RECORD_REFRESH, RequestMethod.POST);
    request.add("adminID", PreferencesHelper.userManager().getUserID());
    request.add("nodeRecordID", nodeRecordID);
    request.add("carNumber", carNumber);
    request.add("pic", pic);
    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        CommonResult result =
            JsonUtil.jsonToObject(response.get(), CommonResult.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getInfo());
          return;
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }
}
